package com.oa.lms.apps.dao.user.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.oa.lms.apps.dao.user.IMailInfoDao;
import com.oa.lms.apps.entity.user.MailInfoValue;

public class MailInfoDaoImpl extends HibernateDaoSupport implements
		IMailInfoDao {
	
	List<MailInfoValue> list;

	@Override
	public Integer add(MailInfoValue mailInfoValue) {
		if (mailInfoValue != null) {
			this.getHibernateTemplate().save(mailInfoValue);
		}
		return mailInfoValue.getMailId();
	}

	@Override
	public void modify(MailInfoValue mailInfoValue) {
		if (mailInfoValue != null) {
			this.getHibernateTemplate().update(mailInfoValue);
		}
	}

	@Override
	public MailInfoValue queryByMailId(Integer mailId) {
		if (mailId != null) {
			return (MailInfoValue) this.getHibernateTemplate().get(MailInfoValue.class, mailId);
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public MailInfoValue queryByMailbox(final String mailBox) {
		if (mailBox != null) {
			String hqlStr = "FROM MailInfoValue mv WHERE mv.mailBox =?";
			final String hql = hqlStr;
			list = this.getHibernateTemplate().executeFind(
					new HibernateCallback() {
						public Object doInHibernate(Session session)
								throws HibernateException, SQLException {
							Query query = session.createQuery(hql);
							query.setString(0, mailBox);
							list = query.list();
							return list;
						}
					});
			return list.get(0);
		}
		return null;
	}

	public List<MailInfoValue> getList() {
		return list;
	}

	public void setList(List<MailInfoValue> list) {
		this.list = list;
	}

	
}
